import java.util.Stack;

class Solution {
    public String decodeString(String s) {
        Stack<String> stringStack = new Stack<>();
        Stack<Integer> intStack = new Stack<>();
        stringStack.push("");
        intStack.push(1);
        int len = s.length();
        char[] sArr = s.toCharArray();
        int i = 0;
        while(i < len){
            if(sArr[i] >= '0' && sArr[i]<= '9'){
                int num = 0;
                while(i < len && sArr[i] >= '0' && sArr[i]<= '9'){
                    num *= 10;
                    num += (sArr[i] - '0');
                    i++;
                }
                intStack.push(num);
            }else if(sArr[i] == '['){
                i++;
                StringBuilder ss = new StringBuilder();
                while(i < len && sArr[i] >= 'a' && sArr[i] <= 'z'){
                    ss.append(sArr[i]);
                    i++;
                }
                stringStack.push(ss.toString());
            }else if(sArr[i] == ']'){
                int num = intStack.pop();
                String ss = stringStack.pop();
                StringBuilder tmp = new StringBuilder();
                while(num > 0){
                    tmp.append(ss);
                    num--;
                }
                stringStack.push(stringStack.pop() + tmp.toString());
                i++;
            }else if(sArr[i] >= 'a' && sArr[i] <= 'z'){
                StringBuilder ss = new StringBuilder();
                while(i < len && sArr[i] >= 'a' && sArr[i] <= 'z'){
                    ss.append(sArr[i]);
                    i++;
                }
                stringStack.push(stringStack.pop() + ss.toString());
            }else{
                i++;
            }
        }
        return stringStack.pop();
    }
}